package com.nifostasky.main.thread.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * Lock锁 -JDK5.0新特性
 */
public class ProminentLock {

    public static void main(String[] args) {
        System.out.println("=======开始: =====================================================================");
        SafeLockThread safeLockThread = new SafeLockThread();
        Thread t1 = new Thread(safeLockThread);
        Thread t2 = new Thread(safeLockThread);
        Thread t3 = new Thread(safeLockThread);

        t1.start();
        t2.start();
        t3.start();

        System.out.println("========结束: ====================================================================");

    }
}

class SafeLockThread implements Runnable{
    private int tickets = 100;
    private ReentrantLock lock = new ReentrantLock();

    @Override
    public void run() {
        while (tickets>0) {
            try {
                //在这里锁住，有点类似同步监视器
                lock.lock();
                if (tickets > 0) {
                    Thread.sleep(100);
                    System.out.println(Thread.currentThread().getName() + ":\t票号:" + tickets + "\t剩余票数:" + --tickets);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                //操作完成共享数据后在这里解锁
                lock.unlock();
            }
        }
    }
}


/**
 *
 *
 * **synchronized和lock的异同:**
 * 1. Lcok是显式锁（需要手动开启和关闭锁），synchronized是隐式锁，出了作用域自动释放。
 * 2. Lock只有代码块锁，synchronized有代码块锁和方法锁。
 * 3. 使用Lcok锁，JVM将花费较少的时间来调度线程，性能更好。并且具有更好的拓展性（提供更多的子类）
 *
 *
 *
 *
 */